// testdll.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "testdll.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

void RunandSetHooktoExe(CString exe_path);

//
//	Note!
//
//		If this DLL is dynamically linked against the MFC
//		DLLs, any functions exported from this DLL which
//		call into MFC must have the AFX_MANAGE_STATE macro
//		added at the very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even before any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//

/////////////////////////////////////////////////////////////////////////////
// CTestdllApp

BEGIN_MESSAGE_MAP(CTestdllApp, CWinApp)
	//{{AFX_MSG_MAP(CTestdllApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestdllApp construction

CTestdllApp::CTestdllApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CTestdllApp object

CTestdllApp theApp;

int myadd(int a, int b)
{
	return a+b;
}

void TestFunc(CString str)
{AFX_MANAGE_STATE(AfxGetStaticModuleState())
	RunandSetHooktoExe(str);
}

LRESULT CALLBACK KeyboardProc(
    int code,
    WPARAM wParam,
    LPARAM lParam
)
{
	CString str;
	str.Format("code = %d, wParam = %d, lParam = %d", code, wParam, lParam);
	
	//AfxMessageBox(str);

	return CallNextHookEx(NULL, code, wParam, lParam);
}

void RunandSetHooktoExe(CString exe_path)
{AFX_MANAGE_STATE(AfxGetStaticModuleState())
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line)
        (LPSTR)(LPCTSTR)exe_path,        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return;
    }

	WaitForInputIdle(pi.hProcess, INFINITE);

    // Wait until child process exits.
    //WaitForSingleObject( pi.hProcess, INFINITE );

	if (NULL == SetWindowsHookEx(WH_KEYBOARD,
								KeyboardProc,
								::GetModuleHandle("testdll.dll"),
								pi.dwThreadId))
	{
		AfxMessageBox("failed!");
	}
    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}